package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Employee;
import com.qf.entity.Menu;
import com.qf.entity.Role;
import com.qf.enums.EmployerExceptionEnum;
import com.qf.exception.EmployerException;
import com.qf.mapper.EmployeeMapper;
import com.qf.mapper.MenuMapper;
import com.qf.mapper.RoleMapper;
import com.qf.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private MenuMapper menuMapper;

    @Override
    public Employee login(String loginName, String loginPassword) {
        Employee param = new Employee();
        param.setLoginName(loginName);
        Employee employee = employeeMapper.selectOne(param);
        if(employee==null){
            log.error("【登陆功能】用户名不存在,loginName={}",loginName);
            throw new EmployerException(EmployerExceptionEnum.USERNAME_NOT_EXISTS);
        }
        if(!employee.getLoginPassword().equals(loginPassword)){
            log.error("【登陆功能】用户密码错误,loginName={}",loginName);
            throw new EmployerException(EmployerExceptionEnum.USER_PASSWORD_ERROR);
        }
        //查询当前用户所对应的角色信息
        Role role = roleMapper.selectRoleById(employee.getRoleId());
        //根据角色信息查询对应的菜单列表
        List<Menu> menuList= menuMapper.selectMenuByRoleId(role.getRoleId());

        //对应菜单列表分类
        ArrayList firstMenu = new ArrayList();//保存一级菜单
        for (Menu menu : menuList) {
            if(menu.getUpMenuId()==0){//一级菜单
                ArrayList secondMenu = new ArrayList();
                for (Menu second : menuList) {
                    if(menu.getMenuId()==second.getUpMenuId()){
                        secondMenu.add(second);//存放二级菜单
                    }
                }
                //将二级菜单封装到一级菜单中
                menu.setSecondMenu(secondMenu);
                //将一级菜单添加到集合中去
                firstMenu.add(menu);

            }
        }
        //将菜单赋值为角色
        role.setMenuList(firstMenu);
        //将角色赋值为用户
        employee.setRole(role);
        //保存该角色的请求列表
        List<Menu> permissionList = menuMapper.selectPermissionByRoleId(role.getRoleId());
        role.setPermissionList(permissionList);
        employee.setRole(role);
        return employee;
    }

    @Override
    public PageInfo<Employee> selectAll(String userName, String roleName, String deptName, Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum,size);
        List<Employee> employeeList = employeeMapper.findEmployeeByContion(userName, roleName, deptName);
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        return pageInfo;
    }

    @Override
    @Transactional
    public void add(Employee employee) {
        employee.setCreateDate(new Date());
        int count = employeeMapper.insertSelective(employee);
        if(count!=1){
            log.error("【添加用户】添加用户失败，employee={}",employee);
            throw new EmployerException(EmployerExceptionEnum.USER_ADD_ERROR);
        }
    }

    @Override
    public Employee updatedById(Integer id) {
        Employee param = new Employee();
        param.setId(id);
        return employeeMapper.selectOne(param);
    }

    @Override
    @Transactional
    public void updated(Employee employee) {
        //修改用户信息
        int count = employeeMapper.updateByPrimaryKeySelective(employee);
        if(count!=1){
            log.error("【用户管理修改】修改用户信息失败，employee={}",employee);
            throw new EmployerException(EmployerExceptionEnum.USER_UPDATED_ERROR);
        }
    }

    @Override
    @Transactional
    public Integer deleteById(Integer id) {
        Employee param = new Employee();
        param.setId(id);
        param.setState(0);
        int count = employeeMapper.updateByPrimaryKeySelective(param);
        return count;
    }

    @Override
    public Integer deletes(Integer[] ids) {
        return employeeMapper.deletes(ids);
    }

    @Override
    public List<Employee> findEmployeeByIds(Integer[] ids) {
        return employeeMapper.findEmployeeByIds(ids);
    }
}
